#include "interp.hpp"
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>

// Define days and weight data for two samples
std::vector<double> days = {0, 6, 10, 13, 17, 20, 28}; // Days since start of the experiment
std::vector<double> sp1 = {6.67, 17.3, 42.7, 37.3, 30.1, 29.3, 28.7}; // Weight data for sample 1
std::vector<double> sp2 = {6.67, 16.1, 18.9, 15.0, 10.6, 9.44, 8.89}; // Weight data for sample 2

int main() {
    // Create Newton interpolation objects to approximate weight trends for both samples
    NewtonInterp interp1(days, sp1);
    NewtonInterp interp2(days, sp2);

    // Flags to track if samples are predicted to "die" (i.e., reach weight < 0) after 28 days
    bool die1 = false;
    bool die2 = false;

    // Directory to save output files
    const std::string output_dir = "output";

    // Output predicted weights for sample 1
    std::string filename1 = "E_1.txt";
    std::ofstream outfile1(output_dir + "/" + filename1);
    if (!outfile1.is_open()) {
        std::cerr << "Failed to open " << filename1 << " for writing." << std::endl;
        return 1;
    }
    // Evaluate weight predictions for days 0 to 43, every 0.1 days
    for (double x = 0; x <= 43; x += 0.1) {
        double pred_W = interp1.evaluate(x);
        if (x > 28 && pred_W < 0) die1 = true; // Check if weight falls below zero after day 28
        outfile1 << x << " " << pred_W << std::endl;
    }

    // Output predicted weights for sample 2
    std::string filename2 = "E_2.txt";
    std::ofstream outfile2(output_dir + "/" + filename2);
    if (!outfile2.is_open()) {
        std::cerr << "Failed to open " << filename2 << " for writing." << std::endl;
        return 1;
    }
    // Evaluate weight predictions for days 0 to 43, every 0.1 days
    for (double x = 0; x <= 43; x += 0.1) {
        double pred_W = interp2.evaluate(x);
        if (x > 28 && pred_W < 0) die2 = true; // Check if weight falls below zero after day 28
        outfile2 << x << " " << pred_W << std::endl;
    }

    // Report if each sample is predicted to die after day 28 based on interpolated weights
    if (die1) {
        std::cout << "Sample 1 is predicted to die after 28 days." << std::endl;
    } else {
        std::cout << "Sample 1 is not predicted to die after 28 days." << std::endl;
    }

    if (die2) {
        std::cout << "Sample 2 is predicted to die after 28 days." << std::endl;
    } else {
        std::cout << "Sample 2 is not predicted to die after 28 days." << std::endl;
    }
    return 0;
}
